//
//  MyNewFeatureViewController.swift
//  weibo08
//
//  Created by 陈杰明 on 16/3/2.
//  Copyright © 2016年 JM. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let countItem = 4

class MyNewFeatureViewController: UICollectionViewController {

    //
    var layout: UICollectionViewFlowLayout = UICollectionViewFlowLayout()
    //实现没有参数的构造函数
    init() {
//     self.collectionView?.backgroundColor = UIColor.redColor()
    super.init(collectionViewLayout: layout)
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        //注册cell
        self.collectionView!.registerClass(MyNewFeatureCell.self, forCellWithReuseIdentifier: reuseIdentifier)
        //设置布局
        layout.itemSize = UIScreen.mainScreen().bounds.size
        layout.minimumLineSpacing = 0
        layout.minimumInteritemSpacing = 0
        layout.scrollDirection = UICollectionViewScrollDirection.Horizontal
        
        collectionView?.bounces = false
        collectionView?.pagingEnabled = true
        //隐藏滚动条
//        collectionView?.showsHorizontalScrollIndicator = false
    }
}

extension MyNewFeatureViewController {
    
    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        // #warning Incomplete implementation, return the number of items
        return countItem
    }
//显示cell
    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath) as! MyNewFeatureCell
    
        // Configure the cell
        cell.itemIndex = indexPath.item
        
//        cell.scaleAnimation()
        
        return cell
    }
    
    override func scrollViewDidEndDecelerating(scrollView: UIScrollView) {
        //获取当前页
        let index = Int(scrollView.contentOffset.x / UIScreen.mainScreen().bounds.width)
        //判断是否是最后一页
        if index == (countItem - 1) {
            let visibleIndexPath = NSIndexPath(forItem: index, inSection: 0)
            if let cell = collectionView?.cellForItemAtIndexPath(visibleIndexPath) as? MyNewFeatureCell {
                cell.scaleAnimation()
            }
        }
    }
}


//自定义cell
class MyNewFeatureCell: UICollectionViewCell {

    //MARK: - 属性
    var itemIndex: Int = 0 {
        didSet{
            bkgImageView.image = UIImage(named: "new_feature_\(itemIndex + 1)")
            startButton.hidden = true
        }
    }
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        prepareUI()
        
     }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    private func scaleAnimation() {
        
        startButton.hidden = false
        //改变大小
        startButton.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(1, delay: 0.1, usingSpringWithDamping: 1, initialSpringVelocity: 5, options: UIViewAnimationOptions(rawValue: 0), animations: { () -> Void in
            
            self.startButton.transform = CGAffineTransformIdentity
            
            }) { (_) -> Void in
                print("动画完成")
        }
        
    }
    //准备加载
    private func prepareUI() {
        //添加子控件
        contentView.addSubview(bkgImageView)
        contentView.addSubview(startButton)
        
        //关闭系统自动布局
        bkgImageView.translatesAutoresizingMaskIntoConstraints = false
        startButton.translatesAutoresizingMaskIntoConstraints = false
        
        //自定义布局
        bkgImageView.snp_makeConstraints { (make) -> Void in
            
            make.edges.equalTo(UIEdgeInsetsZero)
        }
        
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(contentView)
            make.bottom.equalTo(contentView.snp_bottom).offset(-160)
        }
        
    }
    //MARK: - 按钮点击事件
    
    func startButtonClick() {
    //跳转到MainViewController
        AppDelegate.outSwitchRootViewCOntroller(true)
    }
    //MARK: - 懒加载
    //背景
    lazy var bkgImageView: UIImageView = UIImageView(image: UIImage(named: "new_feature_1"))
    //按钮
    lazy var startButton: UIButton = {
    
        let button = UIButton()
        //设置背景图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //设置文字
        button.setTitle("开始使用", forState: UIControlState.Normal)
        //自适应宽高
        button.sizeToFit()
        
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
}
